package com.irheima.edu.manager.controller;

import com.irheima.edu.manager.domain.Student;
import com.irheima.edu.manager.service.StudentService;
import com.sun.javafx.runtime.async.AbstractRemoteResource;

import java.util.Scanner;

//学生管理页面的控制
public class StudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);


    public void start() {
        //不断地给用户操作

        studentLoop:
        while (true) {
            //主菜单搭建
            System.out.println("--------欢迎来到<学生>信息管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choiice = sc.next();
            switch (choiice) {
                case "1":
                    //System.out.println("添加学生");
                    //1控制台只接收
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;

                case "5":
                    System.out.println("退出");

                    break studentLoop;
                default:
                    System.out.println("输出有误");
                    break;

            }

        }
    }
    //修改学生
    private void updateStudent() {
        //调用 获取 得到
        Student[] stus = studentService.findAllStudent();
        //判断
        if (stus == null) {
            System.out.println();
            return;
        }

        System.out.println("请输入你要修改的id");
        String updateId = sc.next();
        boolean exists = studentService.isExists(updateId);
        if (!exists) {
            System.out.println("你输入的id不存在,请重新输入");
        } else {
            System.out.println("请输入学生姓名：");
            String name = sc.next();
            System.out.println("请输入学生年龄：");
            String age = sc.next();
            System.out.println("请输入学生生日：");
            String birthday = sc.next();

            //创建新的学生对象
            Student newStu = new Student();
            newStu.setId(updateId);
            newStu.setName(name);
            newStu.setAge(age);
            newStu.setBirthdy(birthday);
            studentService.updateStudent(updateId, newStu);

            System.out.println("修改成功");
        }


    }

    //删除学生

    public void deleteStudentById() {
        //调用 获取 得到
        Student[] stus = studentService.findAllStudent();
        //判断
        if (stus == null) {
            System.out.println();
            return;
        }
        System.out.println("请输入你要删除的学生学号");
        String delId = sc.next();
        //判断是否相等
        boolean exists = studentService.isExists(delId);
        if (exists) {
            System.out.println();
            studentService.deleteStudentById(delId);
        } else {
            System.out.println("不存在该学号，操作有误");
            ;
        }
    }

    //查看学生
    public void findAllStudent() {
        //调用 获取 得到
        Student[] stus = studentService.findAllStudent();
        //判断
        if (stus == null) {
            System.out.println("暂无数据，请添加！");
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        //遍历
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthdy());
            }
        }
    }

    //添加学生
    public void addStudent() {
        //StudentService studentService = new StudentService();

        //键盘接收
        // Scanner sc = new Scanner(System.in);

        //封装成学生对象
        String id = null;
        while (true) {
            System.out.println("请输入学生id：");
            id = sc.next();
            boolean falg = studentService.isExists(id);
            if (falg) {
                System.out.println("学号被占用");
            } else {
                break;
            }
        }

        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        //将学生信息封装成学生对象
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthdy(birthday);
        //传递业务层 -- 创建Service对象,调用方法

        // 3. 将学生对象,传递给StudentService(业务员)中的addStudent方法
        boolean result = studentService.addStudent(stu);

        //得到的是否添加成功的结果 -- 展示给用户
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }

    }


}

